﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using WgFrame.Net;

public class BuyShopPanel : BasePopupPanel
{
    private int shopId;
    private int buyCount;
    /// <summary>
    /// 名称
    /// </summary>
    private GTextField tfName;
    /// <summary>
    /// 描述
    /// </summary>
    private GTextField tfDescribe;
    /// <summary>
    /// 来源
    /// </summary>
    private GTextField tfSource;
    /// <summary>
    /// 购买数量
    /// </summary>
    private GTextInput tiCount;

    public override void Init()
    {
        base.Init();

        frame = view.GetChild("n1").asCom;
        tfName = frame.GetChild("n3").asTextField;
        tfDescribe = frame.GetChild("n4").asTextField;
        tfSource = frame.GetChild("n5").asTextField;
        tiCount = frame.GetChild("n27").asTextInput;

        GButton btnReset = frame.GetChild("n14").asButton;
        btnReset.onClick.Add(OnReset);
        GButton btnMax = frame.GetChild("n18").asButton;
        btnMax.onClick.Add(OnMax);
        GButton btnJia = frame.GetChild("n21").asButton;
        btnJia.onClick.Add(OnJiaCount);
        GButton btnJian = frame.GetChild("n22").asButton;
        btnJian.onClick.Add(OnJianCount);
        GButton btnCanel = frame.GetChild("n19").asButton;
        btnCanel.onClick.Add(OnCanel);
        GButton btnOk = frame.GetChild("n20").asButton;
        btnOk.onClick.Add(OnOk);
    }

    public override void Show(params object[] para)
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.BuyOk, OnBuyOk);
        this.shopId = (int)para[0];
        this.SetInfo();

        base.Show(para);
        SetBuyCount(0);
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.BuyOk, OnBuyOk);
        base.Close();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_user1").asCom;
    }

    private void SetInfo()
    {
        if (!InitShopMgr.Dict.TryGetValue(shopId, out InitShop initShop))
        {
            return;
        }

        if (initShop.item_type == 1)
        {
            if (!InitItemMgr.Dict.TryGetValue(initShop.item_id, out InitItem initItem))
            {
                return;
            }

            SetBaseInfo(initItem.name, initItem.source, CommonUtil.GetItemColor(initItem.quality));
        }
        else if (initShop.item_type == 2)
        {
            if (!InitEquipMgr.Dict.TryGetValue(initShop.item_id, out InitEquip initItem))
            {
                return;
            }

            SetBaseInfo(initItem.name, string.Empty, CommonUtil.GetEquipColor(initItem.quality));
        }
        else if (initShop.item_type == 3)
        {
            if (!InitHeroMgr.Dict.TryGetValue(initShop.item_id, out InitHero initItem))
            {
                return;
            }

            SetBaseInfo(initItem.name, string.Empty, CommonUtil.GetHeroPinZhiColor(initItem.quality));
        }

    }

    private void SetBaseInfo(string name, string info, Color textColor)
    {
        tfName.text = name;
        tfName.color = textColor;
        tfDescribe.text = info;
        if (string.IsNullOrEmpty(info))
        {
            tfSource.visible = false;
        }
        else
        {
            tfSource.visible = true;
            tfSource.SetVar("text", info).FlushVars();
        }
    }

    private void SetBuyCount(int count)
    {
        buyCount = count;
        tiCount.text = count.ToString();
    }
    private void OnOk(EventContext context)
    {
        if (buyCount < 1)
        {
            FlyTextPanelMgr.Show("购买数量小于1");
            return;
        }
        RequestHelper.SendBuyShopRequest(shopId, buyCount);
    }

    private void OnCanel(EventContext context)
    {
        Close();
    }

    private void OnJianCount(EventContext context)
    {
        buyCount--;
        if (buyCount < 1)
        {
            buyCount = 1;
        }
        SetBuyCount(buyCount);
    }

    private void OnJiaCount(EventContext context)
    {
        buyCount++;
        if (InitShopMgr.Dict.TryGetValue(shopId, out InitShop initShop))
        {
            if (buyCount > initShop.max_count)
            {
                buyCount = initShop.max_count;
            }
        }
        SetBuyCount(buyCount);
    }

    private void OnMax(EventContext context)
    {
        if (!InitShopMgr.Dict.TryGetValue(shopId, out InitShop initShop))
        {
            return;
        }
        SetBuyCount(initShop.max_count);
    }

    private void OnReset(EventContext context)
    {
        SetBuyCount(0);
    }

    private void OnBuyOk(object[] para)
    {
        Close();
    }
}
